import CheckoutForm from "@/src/components/CheckoutForm";
import prisma from "@/src/db/db";
import React from "react";
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

const Purchase = async ({ params: { id } }) => {
  const product = await prisma.product.findUnique({ where: { id } });
  if (!product) {
    return null;
  }
  console.log(product.id, id);
  // 创建支付
  const paymentIntent = await stripe.paymentIntents.create({
    amount: product.priceInCents,
    currency: "USD",
    metadata: {
      productId: product.id,
    },
  });
  if (!paymentIntent.client_secret) {
    throw new Error("Script failed to create payment intent");
  }
  return (
    <CheckoutForm
      product={product}
      clientSecret={paymentIntent.client_secret}
    />
  );
};

export default Purchase;
